# Tab Widget module for CROSS

#########################################################################################
#
# Copyright (C) 2008  
# Authors: Aljosa Osep (aljosa.osep@gmail.com), Jernej Kranjec (jernej.kranjec@gmail.com)
# From Faculty of Electrical Engineering and Computer Science, Maribor, Slovenia
# Done at School of Technology, Seinajoki, Finland
# Under mentor Kimmo Salmenjoki (kimmo.salmenjoki@seamk.fi)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################################

# ==================== imports ====================== #
import pygtk            # import pygtk library
pygtk.require('2.0')    # require version 2.o
import gtk 	        # import gtk


################################
# singleline entry+label widget
################################
class singleLine:
	# methods
	def get_box(self):						# return box
		return self.shbox


	def get_entry(self):						# return entry
		return self.sentry

	def get_entry_text(self):						# return entry
		return self.sentry.get_text()

	def set_entry_text(self, text):
		self.sentry.set_text(text)

	def disable(self):
		self.sentry.set_sensitive(0)

	def enable(self):
		self.sentry.set_sensitive(1)

	def clear(self):
		self.sentry.set_text("")
		

	# initialize
	def __init__(self, title, editable=False, entry_text=""):
		self.shbox = gtk.HBox(False, 0)				# create horizontal box

		self.slabel = gtk.Label(title)				# create title label
		self.shbox.pack_start(self.slabel, True, True, 0)	# pack it
		self.slabel.set_alignment(0,0)				# set its text alignment
		self.slabel.set_size_request(70, 5)
		self.slabel.show()					# show label

		self.sentry = gtk.Entry()				# create entry
		self.shbox.pack_start(self.sentry, True, True, 0)	# pack entry
		self.sentry.set_editable(editable)
		self.sentry.set_text(entry_text)
		self.sentry.show()					# show entry

		self.shbox.show()					# show box


################################
# multipleline entry+label widget
################################
class multipleLine:
	# methods
	def get_box(self):						# return box
		return self.shbox

	def get_entry_text(self):						# return entry
		return self.textbuffer.get_text(self.textbuffer.get_start_iter(),self.textbuffer.get_end_iter())

	def set_entry_text(self, text):
		self.textbuffer.set_text(text)

	def disable(self):
		self.sentry.set_sensitive(0)

	def enable(self):
		self.sentry.set_sensitive(1)

	def clear(self):
		self.textbuffer.set_text("")
		
	# initialize
	def __init__(self, title, editable=False, entry_text=""):
		self.shbox = gtk.HBox(False, 0)				# create horizontal box

		self.slabel = gtk.Label(title)				# create title label
		self.shbox.pack_start(self.slabel, True, True, 0)	# pack it
		self.slabel.set_alignment(0,0)				# set its text alignment
		self.slabel.set_size_request(15, 5)
		self.slabel.show()					# show label

		self.sw = gtk.ScrolledWindow()						# create scrolled window
		self.sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)		# set automatic policy
		self.sentry = gtk.TextView()						# create text view
		self.sentry.set_editable(editable)						# it should not be editable, its just a console
	#	self.sentry.set_size_request(45, 5)
		self.sentry.set_size_request(100,50)

		self.textbuffer = self.sentry.get_buffer()				# textbuffer for texview
		self.sw.add(self.sentry)							# add textview to scroller
		self.shbox.pack_start(self.sw, True, True, 0)	# pack entry
		self.sw.show()								# show scrolled window
		self.sentry.show()							# and show textview

		self.shbox.show()					# show box

#############################################
# singleline radiobuttons+label entry widget
#############################################
class singleLineRadio(singleLine):
	# methods
	def get_mode(self):
		if self.radio1.get_active() == True:
			return 1
		else:
			return 2

	def clear(self):
		pass
	
	# initialize
	def __init__(self, title, r1title = "ASCII", r2title = "HEX"):
		self.shbox = gtk.HBox(False, 0)				# create horizontal box

		self.slabel = gtk.Label(title)				# create title label
		self.shbox.pack_start(self.slabel, True, True, 0)	# pack it
		self.slabel.set_size_request(15, 5)
		self.slabel.set_alignment(0.05,0.1)			# set its text alignment
		self.slabel.show()					# show label

		self.radio1 = gtk.RadioButton(None, r1title)
#		self.radio1.set_active(True)

		self.shbox.pack_start(self.radio1,False, False, 15)
		self.radio1.show()

		self.radio2 = gtk.RadioButton(self.radio1, r2title)
#		self.radio2.set_active(True)

		self.shbox.pack_start(self.radio2, True, True, 15)
		self.radio2.show()

		self.shbox.show()					# show box


##################################
# singleline buttons+label widget
##################################
class singleLineButtons(singleLine):
	# callbacks
	def apply_btn_callback(self, widget):
		self.cb_fnc_ptr()
	
	def clr_btn_callback(self, widget):
		self.clr_cb_fnc(self.frame)

	# methods
	def get_apply_button(self):
		return apply_btn

	def clear(self):
		pass

	# initialize
	def __init__(self, cb_fnc, clr_cb_fnc, frame, stock_1, stock_2 = gtk.STOCK_CLEAR):
		self.cb_fnc_ptr = cb_fnc
		self.clr_cb_fnc = clr_cb_fnc
		self.frame = frame	

		self.shbox = gtk.HBox(True, 10)				# create horizontal box

		apply_btn = gtk.Button(stock=stock_1)
		self.shbox.pack_start(apply_btn, False,False, 0)
		apply_btn.connect("clicked", self.apply_btn_callback)
		apply_btn.show()

		clr_btn = gtk.Button(stock=gtk.STOCK_CLEAR)
		self.shbox.pack_start(clr_btn, False, False, 0)
		clr_btn.connect("clicked", self.clr_btn_callback)
		clr_btn.show()

		self.shbox.show()					# show box	

#########################################
# singleline label+combobox entry widget
#########################################
class singleLineCombo(singleLine):
	# callbacks
	def changed_callback(self, widget):
		if self.combo.get_active() == 2:
			self.t_i_w.set_entry_text("<any>")
			self.t_i_w.disable()
		elif self.combo.get_active() == 1:
			self.t_i_w.enable()	
		elif self.combo.get_active() == 0:	
			self.t_i_w.enable()			

	# methods
	def get_entry_text(self):						# return entry
		return self.combo.get_active_text()

	def clear(self):
		pass

	# initialize
	def __init__(self, title, tag_id_w, editable=False):
		self.t_i_w = tag_id_w

		self.shbox = gtk.HBox(False, 0)				# create horizontal box

		self.slabel = gtk.Label(title)				# create title label
		self.shbox.pack_start(self.slabel, True, True, 0)	# pack it
		self.slabel.set_alignment(0,0)			# set its text alignment
		self.slabel.set_size_request(15, 5)
		self.slabel.show()					# show label

		self.combo = gtk.combo_box_new_text()				# create combo box
		self.shbox.pack_start(self.combo, True, True, 0)	# pack combo
	#	slist = ["IS186B", "EPC0", "EPC1"]			
	#	self.combo.set_popdown_strings(slist)			# set como popdown strings
		self.combo.set_title("Select standard:")
		self.combo.append_text("IS186B   ")
		self.combo.append_text("EPC0")
		self.combo.append_text("EPC1")
		self.combo.connect("changed", self.changed_callback)
		self.combo.set_active(2)
	#	self.combo.entry.set_editable(False)
		self.combo.show()					# show combo

		self.shbox.show()					# show box


#########################################
# singleline label+combobox entry widget
#########################################
class connCombo(singleLine):
	def get_active_index(self):
		return self.combo.get_active()

	# initialize
	def __init__(self, title, items, editable=False, active=0):
		self.shbox = gtk.HBox(False, 0)				# create horizontal box

		self.slabel = gtk.Label(title)				# create title label
		self.shbox.pack_start(self.slabel, True, True, 0)	# pack it
		self.slabel.set_alignment(0,0)				# set its text alignment
		self.slabel.set_size_request(10, 5)
		self.slabel.show()					# show label

		self.combo = gtk.combo_box_new_text()			# create combo box
		self.shbox.pack_start(self.combo, True, True, 0)	# pack combo

		for item in items:					# each item in the tuple
			self.combo.append_text(item)			# append to entry

		self.combo.set_active(active)
		self.combo.show()					# show combo
		self.shbox.show()					# show box


##############################
# tagid widget
##############################
class tagIdWidget(singleLine):
	# methods
	def activate(self, widget, event, data=None):
		if self.sentry.get_text() == "<any>":
			self.sentry.set_text("")

#	def deactivate(self, widget, event, data=None):
#		self.sentry.set_text("<any>")

	#	self.sentry.set_sensitive(1)

	def clear(self):
		self.sentry.set_text("<any>")
		
	# initialize
	def __init__(self, title, editable=False, entry_text=""):
		self.shbox = gtk.HBox(False, 0)				# create horizontal box

		self.slabel = gtk.Label(title)				# create title label
		self.shbox.pack_start(self.slabel, True, True, 0)	# pack it
		self.slabel.set_alignment(0,0)				# set its text alignment
		self.slabel.set_size_request(70, 5)
		self.slabel.show()					# show label

		self.sentry = gtk.Entry()				# create entry
	#	self.sentry.set_sensitive(0)		
		self.sentry.connect("button-press-event", self.activate)
#		self.sentry.connect("button-release-event", self.deactivate)
	#	self.sentry.set_text("<any>")
		self.shbox.pack_start(self.sentry, True, True, 0)	# pack entry
		self.sentry.set_editable(editable)
		self.sentry.set_text(entry_text)
		self.sentry.show()					# show entry

		self.shbox.show()					# show box
	


class custom_frame_widget:
	# methods
	def get_frame(self):
		return self.frame

	def pack_controls(self):
		for ctrl in self.controls:							# for each control in list
			self.fvbox.pack_start(ctrl.get_box(), False, True, 5)			# append control

	def append_widget(self, widget):
		self.controls.append(widget)

	def clear(self):
		for ctrl in self.controls:
			ctrl.clear()
	
	# initialisation
	def __init__(self, title):
		# frame creation
		self.frame = gtk.Frame(title);							# create frame
					# set border size


		self.fvbox = gtk.VBox(False, 5)							# create vertical box for frame
		self.fvbox.set_border_width(5)
		self.frame.add(self.fvbox)							# add it to frame
		self.fvbox.show()								# show vertical box
		self.frame.show()								# and show frame
		self.controls  = []								# list of appended lines






